package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Permission;
import com.itheima.service.IPermissionService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 分页查询，查询所有权限信息
     *
     * @param pageNum   当前页
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAllPermission")
    public ModelAndView findAllPermission(
            @RequestParam(name = "pageNum", required = true, defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", required = true, defaultValue = "3") Integer pageSize) {

        //1、分页查询所有数据
        List<Permission> permissionList = permissionService.findAllPermission(pageNum, pageSize);
        //2、封装分页数据
        PageInfo<Permission> pageInfo = new PageInfo<Permission>(permissionList);

        ModelAndView mav = new ModelAndView();
        mav.addObject("permission",pageInfo);
        mav.setViewName("permission-list");
        return mav;
    }

    @RequestMapping("/insertPermission")
    public String insertPermission(Permission permission){
        permissionService.insertPermission(permission);
        return "redirect:/permission/findAllPermission";
    }


}
